package br.com.buscapecas.model.cursor;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.buscapecas.model.bean.PecaBean;


/**
 * @author Maikon Rafael da Rocha
 *
 */
public class PecaCursor extends BaseCursor {

    /**
     * 
     * @param referencia
     * @param descricao
     * @param marca
     * @return
     * @throws IllegalArgumentException
     * @throws SecurityException
     * @throws SQLException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     * 
     * @author Maikon Rafael da Rocha
     * @since 23/05/2012 13:06:02
     */
    public QueryResult<ArrayList<PecaBean>> buscaPeca(ArrayList<String> pesquisas,
                                                      String codigo,
                                                      ArrayList<String> descricoes,
                                                      Integer codigoMarca) throws IllegalArgumentException, SecurityException, SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        PreparedStatement ps = null;
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT P1.CODIGO, ");
            sql.append("       P1.MARCA, ");
            sql.append("       P1.DESCRICAO, ");
            sql.append("       P1.QUANTIDADE, ");
            sql.append("       P1.VALOR ");
            sql.append("FROM   PECAS P1 ");
            sql.append("WHERE  ");
            if (codigo != null)
                sql.append("       AND P1.CODIGO = ? ");
            if (codigoMarca != null)
                sql.append("       AND P1.MARCA_ID = ? ");
            if (descricoes != null && !descricoes.isEmpty()) {
                StringBuilder frag = new StringBuilder();
                frag.append("(");
                for (int i = 0; i < descricoes.size(); i++) {
                    sql.append("       AND P1.DESCRICAO LIKE ? ");
                }
                frag.append(")");
                sql.append(frag.toString().replaceFirst("AND", ""));
            }
            if (pesquisas != null && !pesquisas.isEmpty()) {
                StringBuilder frag = new StringBuilder();
                frag.append("(");
                for (int i = 0; i < pesquisas.size(); i++) {
                    sql.append(" AND P1.PESQUISA LIKE ? ");
                }
                frag.append(")");
                sql.append(frag.toString().replaceFirst("AND", ""));
            }
            ps = this.conn.prepareStatement(sql.toString());
            int x = 1;
            if (codigo != null)
                ps.setString(x++, codigo);
            if (codigoMarca != null)
                ps.setInt(x++, codigoMarca);
            if (descricoes != null && !descricoes.isEmpty()) {
                for (int i = 0; i < descricoes.size(); i++) {
                    ps.setString(x++, "%"+descricoes.get(x)+"%");
                }
            }
            if (pesquisas != null && !pesquisas.isEmpty()) {
                for (int i = 0; i < pesquisas.size(); i++) {
                    ps.setString(x++, "%"+descricoes.get(x)+"%");
                }
            }
            QueryResult<ArrayList<PecaBean>> retorno = new QueryResult<ArrayList<PecaBean>>();
            BuscaPecasCursor<PecaBean> p = new BuscaPecasCursor<PecaBean>();
            retorno.setResultList(p.getArraylistFromResultSet(PecaBean.class, ps.executeQuery(), true));
            return retorno;
        } finally {
            this.closeSatement(ps);
        }
    }
    
}